package com.hansheng.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hansheng.dao.impl.SemiProductDaoImpl;
import com.hansheng.entity.OutFormEntity;
import com.hansheng.entity.SemiProductEntity;
import com.hansheng.entity.VSemiproductEntity;
import com.hansheng.service.SemiProductService;

import java.util.List;

/**
 * Created by GoldenMan on 2017/5/12.
 */
public class SemiProductServiceImpl implements SemiProductService {
    private SemiProductDaoImpl semiProductDao;
    public void setSemiProductDao(SemiProductDaoImpl semiProductDao) {
        this.semiProductDao = semiProductDao;
    }


    @Override
    public boolean saveSemiProduct(OutFormEntity outFormEntity, JSONArray semiProductDetails) {
        boolean flag=true;
        for (int i = 0; i < semiProductDetails.size(); i++) {
            JSONObject detail=(JSONObject)semiProductDetails.get(i);
            SemiProductEntity semiProductEntity=new SemiProductEntity();
            semiProductEntity.setOutId(outFormEntity.getOutId());
            semiProductEntity.setSid(Integer.parseInt(detail.get("sid").toString()));
            semiProductEntity.setOperator(outFormEntity.getOperator());
            semiProductEntity.setLength(Integer.parseInt(detail.get("length").toString()));
            semiProductEntity.setWidth(Integer.parseInt(detail.get("width").toString()));
            semiProductEntity.setHeight(Integer.parseInt(detail.get("height").toString()));
            semiProductEntity.setCount(Integer.parseInt(detail.get("count").toString()));
            semiProductEntity.setWeightThe(Double.parseDouble(detail.get("weightThe").toString()));
            semiProductEntity.setWeightAct(Double.parseDouble(detail.get("weightAct").toString()));
            semiProductEntity.setStatus(0);
            flag&=semiProductDao.saveSemiProduct(semiProductEntity);
        }
        return flag;
    }

    @Override
    public JSONArray getSemiProductArray(String outFormNo) {
        List spList=semiProductDao.getSemiProductListByOutFormNo(outFormNo);
        JSONArray jsonArray=new JSONArray();
        for (int i = 0; i < spList.size(); i++) {
            JSONObject tempObject=new JSONObject();
            VSemiproductEntity semiproductEntity=(VSemiproductEntity)spList.get(i);
            tempObject.put("outFormNo",semiproductEntity.getOutFormNo());
            tempObject.put("outId",semiproductEntity.getOutId());
            tempObject.put("sid",semiproductEntity.getSid());
            tempObject.put("msid",semiproductEntity.getMsid());
            tempObject.put("msNo",semiproductEntity.getMsNo());
            tempObject.put("spid",semiproductEntity.getSpid());
            tempObject.put("length",semiproductEntity.getLength());
            tempObject.put("width",semiproductEntity.getWidth());
            tempObject.put("height",semiproductEntity.getHeight());
            tempObject.put("count",semiproductEntity.getCount());
            tempObject.put("weightThe",semiproductEntity.getWeightThe());
            tempObject.put("weightAct",semiproductEntity.getWeightAct());
            tempObject.put("finishedTime",semiproductEntity.getFinishedTime());
            tempObject.put("name",semiproductEntity.getName());
            tempObject.put("position",semiproductEntity.getPosition());
            jsonArray.add(tempObject);
        }
        return jsonArray;
    }


}
